/**
*铁塔 js
*/ 

var	ironTowerDS;
var ironTowerFormPanel;
function doIronTowerDelete(id)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该信息?', 
	function(btn) {
		if(btn == 'yes') // 选中了是按钮	
		{
			JIronTowerWeb.deleteIronTowerBO(id,function(data)
			{
				if(data)
				{ 
					Ext.Msg.alert('成功',"操作成功！"); 
					ironTowerDS.load({params:{start:0,limit:17,'<%=Constant.BTS_CODE%>':'<%=strbtscode%>'}});	 
					//ironTowerFormPanel.getForm().reset();			
				}else
				{
					Ext.Msg.alert("失败","操作失败!");
				}
			})
		}
	})	
}


Ext.onReady(function(){  
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

	var irontowerType = Ext.data.Record.create([								
							   {name: 'id', type: "string"},
							   {name: 'btscode', type: "string"},
							   {name: 'btsname', type: "string"},
							   {name: 'property', type: "string"},
							   {name: 'opentime', type: "date"},
							   {name: 'fromunit', type: "string"}, 
							   {name: 'contact', type: "string"}, 
							   {name: 'telphone', type: "string"}, 
							   {name: 'type', type: "string"}, 
							   {name: 'height', type: "string"}, 
							   {name: 'warnschilder', type: "string"}, 
							   {name: 'maintenance', type: 'string'},
							   {name: 'refreshtime',  type: 'date'},
							   {name: 'memo',  type: 'string'},
							   {name: 'itowerPlatNum',  type: 'string'},
							   {name: 'itowerSta',  type: 'string'}
						 ]);
	var irontowerCM = new Ext.grid.ColumnModel(
			[
				new Ext.grid.CheckboxSelectionModel({singleSelect: true}), 	            
				{header: "流水号", sortable: true, dataIndex: 'id',align:'left'},
				{header: "基站编号", sortable: true, dataIndex: 'btscode',align:'left'},
				{header: "基站名称",  sortable: true, dataIndex: 'btsname',align:'left'},	
				{header: "租用或购买何单位",  sortable: true, dataIndex: 'fromunit',align:'left'},	
				{header: "类型",  sortable: true, dataIndex: 'type',align:'left'}, 					    
				{header: "启用日期",  width: 150,sortable: true, renderer: Ext.util.Format.dateRenderer('Y-m-d'),dataIndex: 'opentime',align:'left'}				
			]
		);				 
	
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doIronTowerDelete(\"" + id  +   "\")'>";  
	}
	
	function formatFence(value) 
	{
		var strState = "";
		if(value == "0")
			strState = '<span style="color:red;">' + "否" + '</span>';
		else  
			strState = '是'; 		
		return strState;
	}
	
	ironTowerDS = new Ext.data.Store({
				proxy: new Ext.data.DWRProxy(JIronTowerWeb.getIronTowerBOList, true),	
				reader: new Ext.data.ListRangeReader({
					totalProperty: 'totalSize',
					id: 'id'
				}, irontowerType),
				sortInfo:{field:'id', direction:'ASC'}// 排序信息
			});

	
	var towerMountedGridDiv = Ext.get("mainTabPanelDIV");
	var irontowerGrid = new Ext.grid.GridPanel({			 	
			ds: ironTowerDS,
			cm: irontowerCM, 
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) {
                            ironTowerFormPanel.getForm().loadRecord(rec);
                        }
                    }
                }),
			autoWidth : true,  
			autoScroll : true,  
			autoHeight　: false,
			region : 'center',
			height: 400,
			minSize: 200,		
			frame:true,
			collapsible:false, 
			enableDragDrop:false, 		
			bbar: new Ext.PagingToolbar({
					pageSize: 17,
					store: ironTowerDS,
					displayInfo: true,
					displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
					emptyMsg: "没有记录"
				})
		});

	ironTowerDS.on('load', function() {
		irontowerGrid.getSelectionModel().selectFirstRow(); 
	});		
	
	var propertyStore  = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	
	var propertyCombo = new Ext.form.ComboBox({	   
		fieldLabel: '产权',
		name: 'property',	
		width:150,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'property',
		triggerAction: 'all', 
		editable:false, 
		disabled:true,
		store: propertyStore
	});
	propertyStore.on('beforeload', function() {
			  propertyStore.baseParams = {
					DIRECTORY_TYPE:'15'   
			  };
		});	
	propertyStore.load({params:{DIRECTORY_TYPE:'15'}});

	
	var typeStore  = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	
	var typeCombo = new Ext.form.ComboBox({	   
		fieldLabel: '类型',
		name: 'type',	
		width:150,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'type',
		triggerAction: 'all', 
		editable:false,
		disabled:true,		
		store: typeStore
	});
	typeStore.on('beforeload', function() {
			  typeStore.baseParams = {
					DIRECTORY_TYPE:'16'   
			  };
		});	
	typeStore.load({params:{DIRECTORY_TYPE:'16'}});
	
	
	var itowerPlatNumStore  = new Ext.data.Store({  
			proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
			reader:new Ext.data.DWRArrayReader(
					{},
					new Ext.data.Record.create([  
						{name: 'text', mapping:1},       
						{name: 'value',mapping:0}                   
					  ])
					)  
		});
	var itowerPlatNumCombo = new Ext.form.ComboBox({	   
		fieldLabel: '铁塔警示牌',
		name: 'itowerPlatNum',	
		width:150,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'itowerPlatNum',
		triggerAction: 'all', 
		editable:false, 
		disabled:true,
		store: itowerPlatNumStore
	});
	itowerPlatNumStore.on('beforeload', function() {
			  itowerPlatNumStore.baseParams = {
					DIRECTORY_TYPE:'07'   
			  };
		});	
	itowerPlatNumStore.load({params:{DIRECTORY_TYPE:'07'}});
	
	
	
	ironTowerFormPanel = new Ext.form.FormPanel({
		region     : 'south', 
		bodyStyle  : 'padding: 10px; background-color: #DFE8F6',
		labelWidth : 130,  
		height: 190,
		minSize: 200,
		maxSize: 200,
		frame:true,
		buttonAlign:'right',
		layout:'column', 
		items:[
			 {
				columnWidth:.33,
				layout: 'form',  
				labelWidth:100,	 
				labelAlign:'right',	
				items: [
					{
						xtype:'hidden',
						fieldLabel: '流水号',
						width:150,
						name: 'id'
					}, {
						xtype:'textfield',
						readOnly: true,
						fieldLabel: '基站编号',
						width:150,
						disabled:true,
						name: 'btscode'
					}, {
						xtype:'textfield',
						fieldLabel: '基站名称',
						width:150,
						disabled:true,
						readOnly: true,
						name: 'btsname'
					},
					propertyCombo
					, {
						xtype:'datefield',
						fieldLabel: '启用时间',
						width:150,
						disabled:true,
						format : "Y-m-d",
						allowBlank:false,
						name: 'opentime'
					}, {
						xtype:'textfield',
						fieldLabel: '租用或购买何单位',
						width:150,
						disabled:true,
						name: 'fromunit'
					}
					]
			 },
			 {
				columnWidth:.33,
				layout: 'form',  
				labelWidth:110,	 
				labelAlign:'right',	
				items: [
					{
						xtype:'textfield',
						fieldLabel: '联系人',
						width:150,
						disabled:true,
						name: 'contact'
					}, {
						xtype:'textfield',
						fieldLabel: '联系电话',
						width:150,
						disabled:true,
						name: 'telphone'
					}
					, typeCombo
					, {
						xtype:'textfield',
						fieldLabel: '高度（米）',
						width:150,
						value:1,
						disabled:true,
						name: 'height'
					}, {
						xtype:'textfield',
						fieldLabel: '平台数量级',
						width:150,
						disabled:true,
						name: 'warnschilder'
					}]
			 },
			 {
				columnWidth:.33,
				layout: 'form',  
				labelWidth:110,	 
				labelAlign:'right',	
				items: [
					{
						xtype:'textfield',
						fieldLabel: '锈蚀情况',
						width:150,
						disabled:true,
						name: 'itowerSta'
					},
					itowerPlatNumCombo,
					{	
						xtype:'textfield',
						fieldLabel: '铁塔整治及维修记录',
						width:150,
						disabled:true,
						name: 'maintenance'
					}					
					,{
						xtype:'textarea',
						fieldLabel: '备注',
						width:150,
						disabled:true,
						name: 'memo'
					}]
			 }
			] 	
	});



	//Simple 'border layout' panel to house both grids
	
	var displayPanel = new Ext.Panel({
		width    : towerMountedGridDiv.getComputedWidth(),
		height   : 500,
		layout   : 'border',
		renderTo : 'irontowerTab',
		items    : [
			irontowerGrid,
			ironTowerFormPanel
		]
	});
	
	
	function saveIronTowerBO()
	{
		if (!ironTowerFormPanel.getForm().isValid())
			return;	
			
		var form = ironTowerFormPanel.getForm().getValues(false);	
		IronTowerBO = {};
		IronTowerBO.id = form.id;
		IronTowerBO.btscode = form.btscode;
		IronTowerBO.btsname= form.btsname;
		IronTowerBO.property= form.property;
		IronTowerBO.fromunit = form.fromunit;
		IronTowerBO.contact = form.contact;
		IronTowerBO.telphone = form.telphone;
		IronTowerBO.type = form.type;
		IronTowerBO.height = form.height;		
		IronTowerBO.warnschilder = form.warnschilder;
		IronTowerBO.maintenance = form.maintenance;
		IronTowerBO.memo = form.memo; 		
		IronTowerBO.itowerPlatNum = form.itowerPlatNum;		 
		IronTowerBO.itowerSta = form.itowerSta;		 
		
		var opentimeDate = new Date();	
		var arrPlanTime = form.opentime.split("-");		
				
		var yearOth = arrPlanTime[0];
		var monthOth = arrPlanTime[1];
		var dayOth = arrPlanTime[2];
		opentimeDate.setFullYear(yearOth,monthOth-1,dayOth);
		
		IronTowerBO.opentime = opentimeDate;
	   			
		JIronTowerWeb.saveIronTowerBO(IronTowerBO,function(data)
		{
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				loadDate(ironTowerDS,ironTowerFormPanel); 
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}
		})
			
	}
	
	loadDate(ironTowerDS,ironTowerFormPanel);
	
})

